﻿using System.Collections;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using Radzen;
using System.Linq;
using System.Linq.Dynamic.Core;
using Swimj.Core.Utilities;

namespace Swimj.RazorLib.Components;

public partial class DropDownTree<TValue> : DropDownBase<TValue>
{
    [Parameter] public object SelectedValue { get; set; }

    [Parameter] public int MaxSelectedLabels { get; set; } = 4;

    [Parameter] public bool Chips { get; set; }

    [Parameter] public string SelectedItemsText { get; set; } = "items selected";

    [Parameter] public RenderFragment<dynamic> ValueTemplate { get; set; }

    [Parameter] public bool OpenOnFocus { get; set; }

    //[Parameter] public IEnumerable Data { get; set; }

    //[Parameter]
    //public string TextProperty { get; set; }

    [Parameter] public string ChildrenProperty { get; set; }

    [Parameter] public Func<object, bool> HasChildren { get; set; }

    [Parameter] public Func<IEnumerable, object, object>? GetItemFromTree { get; set; }

    private async Task OnFocus(Microsoft.AspNetCore.Components.Web.FocusEventArgs args)
    {
        if (OpenOnFocus)
        {
            await OpenPopup("Enter", false);
        }
    }

    protected override async Task OpenPopup(string key = "ArrowDown", bool isFilter = false, bool isFromClick = false)
    {
        if (Disabled)
            return;

        await JSRuntime.InvokeVoidAsync(OpenOnFocus ? "Radzen.openPopup" : "Radzen.togglePopup", Element, PopupID,
            true);

        if (list != null)
        {
            await JSRuntime.InvokeVoidAsync("Radzen.selectListItem", search, list, selectedIndex);
        }
    }

    private async Task OnChipRemove(object item)
    {
        if (!Disabled)
        {
            await OnSelectItem(item);
        }
    }

    async System.Threading.Tasks.Task Clear()
    {
        if (Disabled)
            return;

        // searchText = null;
        // internalValue = default(TValue);
        // selectedItem = null;
        //
        // selectedItems.Clear();
        //
        // await ValueChanged.InvokeAsync((TValue)internalValue);
        // if (FieldIdentifier.FieldName != null) { EditContext?.NotifyFieldChanged(FieldIdentifier); }
        // await Change.InvokeAsync(internalValue);
        //
        // await grid.Reload();

        StateHasChanged();
    }

    protected override string GetComponentCssClass()
    {
        return GetClassList("rz-dropdown").Add("rz-dropdown-chips", Chips && selectedItems.Count > 0)
            .Add("rz-clear", AllowClear).ToString();
    }

    async Task ValueChangedHandler(object item)
    {
        if (!Disabled && !Multiple)
        {
            await JSRuntime.InvokeVoidAsync("Radzen.closePopup", PopupID);
        }

        await SelectItem(item);
    }

    protected override Task OnParametersSetAsync()
    {
        SelectItemFromValue(Value);
        return base.OnParametersSetAsync();
    }

    protected override void SelectItemFromValue(object value)
    {
        if (GetItemFromTree != null)
        {
            SelectedValue = GetItemFromTree(Data, value);
        }
    }
}